/**
 * @file index.js
 * @author swan
 */
const app = getApp()
var timer = null
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var bdApi = require('../../utils/bdApi.js');
var bdRequest = require('../../utils/bdRequest.js')
import config from '../../utils/config.js'
var pageCount = config.getPageCount;
var WxParse = require('../../wxParse/wxParse.js');
import seoConfig from '../../seoConfig/config.js'
Page({
    data: {
        userInfo: {},
        hasUserInfo: false,
        name: '',
        time: '',
        moveDistance: 65,
        peoples: 160,
        scrollList: [
            {
                "name": "张均",
                "mobile": "18901777885"
            },
            {
                "name": "邓志强",
                "mobile": "15112758277"
            },
            {
                "name": "梁西",
                "mobile": "18684664310"
            },
            {
                "name": "张军",
                "mobile": "15236699876"
            },
            {
                "name": "欧明岳",
                "mobile": "18869863881"
            },
            {
                "name": "陈贤亮",
                "mobile": "18086086566"
            },
            {
                "name": "沈刚",
                "mobile": "18907155693"
            },
            {
                "name": "李冬梅",
                "mobile": "13225761041"
            },
            {
                "name": "吕鹏",
                "mobile": "15131184536"
            },
            {
                "name": "陈天斌",
                "mobile": "15224632839"
            },
            {
                "name": "周龙",
                "mobile": "15755515920"
            },
            {
                "name": "龙敬",
                "mobile": "18173960660"
            },
            {
                "name": "傅靓",
                "mobile": "18956022763"
            },
            {
                "name": "测试",
                "mobile": "13131313131"
            },
            {
                "name": "邓玲慧",
                "mobile": "15675846208"
            },
            {
                "name": "曾彪",
                "mobile": "18670350239"
            },
            {
                "name": "张冠男",
                "mobile": "15111224934"
            },
            {
                "name": "刘海鹏",
                "mobile": "15575990245"
            },
            {
                "name": "贺心晴",
                "mobile": "18374899731"
            },
            {
                "name": "石bin",
                "mobile": "13272424856"
            },
            {
                "name": "曾诚",
                "mobile": "15208388287"
            },
            {
                "name": "马雯",
                "mobile": "15283844204"
            },
            {
                "name": "马勤宇",
                "mobile": "15828528019"
            },
            {
                "name": "谢殷英",
                "mobile": "13348827725"
            },
            {
                "name": "赵凯强",
                "mobile": "14729222804"
            },
            {
                "name": "杨哲",
                "mobile": "18292451346"
            },
            {
                "name": "熊波",
                "mobile": "15676393127"
            },
            {
                "name": "刘平玉",
                "mobile": "18676742460"
            },
            {
                "name": "章清芳",
                "mobile": "15608096686"
            },
            {
                "name": "胡巧丽",
                "mobile": "15527013145"
            },
            {
                "name": "吴学林",
                "mobile": "18715104306"
            },
            {
                "name": "马其放",
                "mobile": "18205083925"
            },
            {
                "name": "杨柳辉",
                "mobile": "13138188623"
            },
            {
                "name": "龚良晨",
                "mobile": "13870982564"
            },
            {
                "name": "何琳琳",
                "mobile": "15986844539"
            },
            {
                "name": "王飞翔",
                "mobile": "18684643276"
            },
            {
                "name": "杜水林",
                "mobile": "13550196783"
            },
            {
                "name": "大大",
                "mobile": "13131313131"
            },
            {
                "name": "雷淞滟",
                "mobile": "18280295524"
            },
            {
                "name": "杨尉文",
                "mobile": "13825279401"
            },
            {
                "name": "喻明空",
                "mobile": "18613221233"
            },
            {
                "name": "何立鹏",
                "mobile": "18660026131"
            },
            {
                "name": "李威",
                "mobile": "13720182511"
            },
            {
                "name": "郭瑞",
                "mobile": "15773182586"
            },
            {
                "name": "杨文艳",
                "mobile": "18710849843"
            },
            {
                "name": "陈镝宇",
                "mobile": "15982222519"
            },
            {
                "name": "汪新洁",
                "mobile": "15905602366"
            },
            {
                "name": "罗云波",
                "mobile": "13187311845"
            },
            {
                "name": "龚明",
                "mobile": "18672979350"
            },
            {
                "name": "刘力华",
                "mobile": "15070018742"
            },
            {
                "name": "禹淞文",
                "mobile": "15914095870"
            },
            {
                "name": "罗卫平",
                "mobile": "18684651610"
            },
            {
                "name": "吕志胜",
                "mobile": "18814483412"
            },
            {
                "name": "刘秦",
                "mobile": "13484545315"
            },
            {
                "name": "啊啊",
                "mobile": "1313131"
            },
            {
                "name": "的的",
                "mobile": "13131313131"
            },
            {
                "name": "方琴",
                "mobile": "15397819556"
            },
            {
                "name": "杨刚",
                "mobile": "15874992102"
            },
            {
                "name": "啊啊",
                "mobile": "13131313131"
            },
            {
                "name": "李俊",
                "mobile": "18602884396"
            },
            {
                "name": "吴凯",
                "mobile": "15874141875"
            },
            {
                "name": "徐中腾",
                "mobile": "13593821551"
            },
            {
                "name": "刘晴",
                "mobile": "18779862112"
            },
            {
                "name": "郎慧",
                "mobile": "18428386493"
            },
            {
                "name": "杨佳南",
                "mobile": "15129256678"
            },
            {
                "name": "李芳",
                "mobile": "18683614042"
            },
            {
                "name": "朱博文",
                "mobile": "13487277915"
            },
            {
                "name": "鞠亮",
                "mobile": "18482112397"
            },
            {
                "name": "向其明",
                "mobile": "18972004286"
            },
            {
                "name": "陈贤亮",
                "mobile": "13971119323"
            },
            {
                "name": "吴双",
                "mobile": "13545423042"
            },
            {
                "name": "王森林",
                "mobile": "18001386421"
            },
            {
                "name": "祝琳欣",
                "mobile": "15881028355"
            },
            {
                "name": "段闰平",
                "mobile": "15700791759"
            },
            {
                "name": "阮甜",
                "mobile": "13871986286"
            },
            {
                "name": "刘兵兵",
                "mobile": "13018088200"
            },
            {
                "name": "张远",
                "mobile": "15249285138"
            },
            {
                "name": "测试",
                "mobile": "13131313131"
            },
            {
                "name": "啊啊",
                "mobile": "13131111111"
            },
            {
                "name": "程洪桥",
                "mobile": "13258262991"
            },
            {
                "name": "杨",
                "mobile": "15855127802"
            },
            {
                "name": "的的",
                "mobile": "13133111111"
            },
            {
                "name": "张娟",
                "mobile": "18907155555"
            },
            {
                "name": "张军t",
                "mobile": "18907155632"
            },
            {
                "name": "邓婷匀",
                "mobile": "15116129573"
            },
            {
                "name": "吕建英",
                "mobile": "18019982151"
            },
            {
                "name": "夏拉萨",
                "mobile": "13251312740"
            },
            {
                "name": "陈宇飞",
                "mobile": "17730037260"
            },
            {
                "name": "李贵平",
                "mobile": "13588046134"
            },
            {
                "name": "张荣",
                "mobile": "18086502179"
            },
            {
                "name": "周玉芬",
                "mobile": "13319513176"
            },
            {
                "name": "陈益龙",
                "mobile": "18755179475"
            },
            {
                "name": "钱起",
                "mobile": "18288665918"
            },
            {
                "name": "谭伟",
                "mobile": "15284493363"
            },
            {
                "name": "黄慧慧",
                "mobile": "13866727502"
            },
            {
                "name": "黄瑞达",
                "mobile": "18682251280"
            },
            {
                "name": "刘科科",
                "mobile": "18673128582"
            },
            {
                "name": "刘林海",
                "mobile": "18672321028"
            },
            {
                "name": "陶文林",
                "mobile": "15656590131"
            },
            {
                "name": "万娇",
                "mobile": "15073145752"
            },
            {
                "name": "周卓阳",
                "mobile": "15580026425"
            },
            {
                "name": "黄贵林",
                "mobile": "18683245456"
            },
            {
                "name": "孙凯",
                "mobile": "18255190281"
            },
            {
                "name": "谢艳丽",
                "mobile": "13632716225"
            },
            {
                "name": "吴昊",
                "mobile": "15116414413"
            },
            {
                "name": "杨云通",
                "mobile": "15380831292"
            },
            {
                "name": "邓薇",
                "mobile": "13080672780"
            },
            {
                "name": "王艳玲",
                "mobile": "15828531562"
            },
            {
                "name": "鲍威",
                "mobile": "13419648030"
            },
            {
                "name": "谢军",
                "mobile": "18907155236"
            },
            {
                "name": "柳月",
                "mobile": "17771848054"
            },
            {
                "name": "丁本",
                "mobile": "15755109828"
            },
            {
                "name": "杨丹妮",
                "mobile": "18379134636"
            },
            {
                "name": "谭庆国",
                "mobile": "13107113514"
            },
            {
                "name": "唐桂林",
                "mobile": "18373637042"
            },
            {
                "name": "王亚宁",
                "mobile": "15955183732"
            },
            {
                "name": "苏梦雪",
                "mobile": "18321780023"
            },
            {
                "name": "刘祖赢",
                "mobile": "13723886215"
            },
            {
                "name": "黄穗",
                "mobile": "15118089187"
            },
            {
                "name": "徐嘉诺",
                "mobile": "13055199501"
            },
            {
                "name": "郑丕安",
                "mobile": "18079179762"
            },
            {
                "name": "刘宇嘉",
                "mobile": "15674973303"
            },
            {
                "name": "陈路瑶",
                "mobile": "15655179310"
            },
            {
                "name": "姜杰文",
                "mobile": "18779178353"
            },
            {
                "name": "梅路",
                "mobile": "18711093145"
            },
            {
                "name": "胡元冲",
                "mobile": "15071184572"
            },
            {
                "name": "喻亮亮",
                "mobile": "15070927542"
            },
            {
                "name": "姜雨晴",
                "mobile": "15195890677"
            },
            {
                "name": "李健",
                "mobile": "15209885932"
            },
            {
                "name": "何炎梅",
                "mobile": "17729919527"
            },
            {
                "name": "雷军",
                "mobile": "15055194303"
            },
            {
                "name": "王晨霞",
                "mobile": "15256906435"
            },
            {
                "name": "唐凯锋",
                "mobile": "18119540331"
            },
            {
                "name": "肖勇",
                "mobile": "18670718022"
            },
            {
                "name": "黄文妹",
                "mobile": "13929537424"
            },
            {
                "name": "刘炼",
                "mobile": "18684782617"
            },
            {
                "name": "董建军",
                "mobile": "18356091194"
            },
            {
                "name": "刘倩蕾",
                "mobile": "15918819636"
            },
            {
                "name": "沐霈",
                "mobile": "15692026130"
            },
            {
                "name": "刘司鼎",
                "mobile": "15700728429"
            },
            {
                "name": "李岩",
                "mobile": "18860478736"
            },
            {
                "name": "邓佳康",
                "mobile": "15274982180"
            },
            {
                "name": "沈心怡",
                "mobile": "13966694013"
            },
            {
                "name": "岳尧",
                "mobile": "18381302572"
            },
            {
                "name": "文蜜",
                "mobile": "18708133923"
            },
            {
                "name": "余若",
                "mobile": "13430656115"
            },
            {
                "name": "李佳",
                "mobile": "13049498185"
            },
            {
                "name": "吴俊鑫",
                "mobile": "15986691835"
            },
            {
                "name": "王潇",
                "mobile": "13980674084"
            },
            {
                "name": "许碧清",
                "mobile": "18926496439"
            },
            {
                "name": "邝思琪",
                "mobile": "15622184241"
            },
            {
                "name": "胡甜甜",
                "mobile": "18827628104"
            },
            {
                "name": "徐卫华",
                "mobile": "15817454815"
            },
            {
                "name": "张曾益",
                "mobile": "13713837234"
            },
            {
                "name": "陈川",
                "mobile": "18408287255"
            },
            {
                "name": "郑清",
                "mobile": "15899877847"
            },
            {
                "name": "樊东海",
                "mobile": "18351983721"
            },
            {
                "name": "杨有为",
                "mobile": "15656547137"
            },
            {
                "name": "郑梅",
                "mobile": "13049663425"
            },
            {
                "name": "谢军辉",
                "mobile": "18684832609"
            },
            {
                "name": "尧玉君",
                "mobile": "13016180574"
            },
            {
                "name": "李文俊",
                "mobile": "13726805019"
            },
            {
                "name": "杨崇信",
                "mobile": "18368402165"
            },
            {
                "name": "赵泉",
                "mobile": "18508420875"
            },
            {
                "name": "成音健",
                "mobile": "18684855156"
            },
            {
                "name": "张峰",
                "mobile": "13865969499"
            },
            {
                "name": "侯芬",
                "mobile": "18215521115"
            },
            {
                "name": "韦洁梅",
                "mobile": "18813299911"
            },
            {
                "name": "邓韵诗",
                "mobile": "18344408613"
            },
            {
                "name": "陈志远",
                "mobile": "18084819717"
            },
            {
                "name": "陈晓纯",
                "mobile": "13229625675"
            },
            {
                "name": "胡不为",
                "mobile": "15605197510"
            },
            {
                "name": "曾玮",
                "mobile": "13539913566"
            },
            {
                "name": "匡泉",
                "mobile": "13798002559"
            },
            {
                "name": "何学斌",
                "mobile": "15955169225"
            },
            {
                "name": "高锐鹏",
                "mobile": "18826077802"
            },
            {
                "name": "王冬冬",
                "mobile": "18408226654"
            },
            {
                "name": "李卓思",
                "mobile": "18819470919"
            },
            {
                "name": "全",
                "mobile": "15622775538"
            },
            {
                "name": "蒋晓峰",
                "mobile": "13242436860"
            },
            {
                "name": "印飞翔",
                "mobile": "15274893208"
            },
            {
                "name": "潘婉仪",
                "mobile": "15626251256"
            },
            {
                "name": "田雪彬",
                "mobile": "18200178913"
            },
            {
                "name": "彭恩华",
                "mobile": "15918548045"
            },
            {
                "name": "王涛",
                "mobile": "18374977632"
            }],
        copyList: [],
        schoolList: ['武汉大学', '肇庆学院', '黄山学院', '贵州轻工', '四川音乐学院', '郑州航空工业', '湖北第二师范学院', '泉州市华光职业技术学院', '上饶市艺术学校'],
        index: 0,
        listLenght: 0,
        animStyle: {
            transition: 'all 0.5s',
            'margin-top': 0
        },
        height: '300rpx'
    },
    onLoad: function (options) {
        let list = this.data.scrollList

        for (let item of list) {
            let num = util.randomNum(0, 8)
            let myphone = item.mobile.substr(3, 4)
            let myname = item.name.substr(0, 1)
            item.name = item.name.length <= 1 ? myname : item.name.length === 2 ? myname + '*' : myname + "**";
            item.school = this.data.schoolList[num]
            item.mobile = item.mobile.replace(myphone, "****");
            item.time = new Date().getFullYear() + '-' + Number(new Date().getMonth() + 1)
        }
        this.setData({
            scrollList: list
        })
        this.setData({
            listLenght: this.data.scrollList.length,
            copyList: this.data.scrollList,
            index: 0
        })
        timer = setInterval(() => {
            this.scroll();
        }, 2000)
        this.setConfig(seoConfig.dacxConfig)
    },
    onUnload() {
        clearInterval(timer)
        timer = null
    },
    //获得文章分类
    setConfig(seo) {
        swan.setPageInfo && swan.setPageInfo(seo)
    },
    formSubmit: function (e) {
        console.log('form发生了submit事件，携带数据为：', e.detail.value);
        let data = e.detail.value
        if (!data.name || !data.tel || !data.school || !this.data.time) {
            swan.showToast({
                title: '请填写所有必填项',
                duration: 1000,
            });
        }
        var url = Api.postFormUrl();
        var ip = new Date().getTime() + Math.round(Math.random() * 10000); // 小程序端无法获取ip
        var content = `类&nbsp;&nbsp;型：档案查询\n姓&nbsp;&nbsp;名：${data.name}\n联系电话：${data.tel}\n毕业院校：${data.school}\n毕业时间：${this.data.time}\n来源： 办得爽百度小程序`
        var postLikeRequest = bdRequest.postRequest(url, { title: "办得爽小程序-全国档案查询系统", tel: data.tel, name: data.name, content: content, "from": '百度小程序', ip: ip });
        postLikeRequest
            .then(response => {
                if (response.data.status == '200') {
                    swan.showToast({
                        icon: 'success',
                        title: '提交成功'
                    });
                    this.setData({
                        name: ''
                    })

                }
            })
    },
    bindTimeChange(e) {
        this.setData({
            time: e.detail.value
        })
    },
    getUserInfo(e) {
        swan.login({
            success: () => {
                swan.getUserInfo({
                    success: (res) => {
                        this.setData({
                            userInfo: res.userInfo,
                            hasUserInfo: true
                        });
                    },
                    fail: () => {
                        this.setData({
                            userInfo: e.detail.userInfo,
                            hasUserInfo: true
                        });
                    }
                });
            },
            fail: () => {
                swan.showModal({
                    title: '未登录',
                    showCancel: false
                });
            }
        });
    },
    playPhone() {
        swan.makePhoneCall({
            phoneNumber: '4009687088' //仅为示例，并非真实的电话号码
        });
    },
    //获取文章列表数据
    fetchPostsData: function (data) {
        var self = this;
        if (!data) data = {};
        if (!data.page) data.page = 1;
        if (!data.categories) data.categories = 0;
        if (!data.search) data.search = '';
        if (data.page === 1) {
            self.setData({
                postsList: []
            });
        };
        swan.showLoading({
            title: '正在加载',
            mask: true
        });
        if (data.categories === 0) {
            var getPostsRequest = bdRequest.getRequest(Api.getPosts(data));
        } else {
            var getPostsRequest = bdRequest.getRequest(Api.getPostsByIDs(data));
        }
        getPostsRequest
            .then(response => {
                if (response.statusCode === 200) {
                    for (var i = 0; i < response.data.length; i++) {
                        // console.log(response.data[i].excerpt.rendered);
                        var rendered = response.data[i].excerpt.rendered;
                        if (rendered === '') {
                            continue
                        }
                        var res = WxParse.bdParse('rendered', 'html', rendered, self, 5, true);
                        // console.log(res);
                        response.data[i].excerpt.rendered = res;
                    }
                    if (response.data.length < pageCount) {
                        self.setData({
                            isLastPage: true
                        });
                    } else {
                        self.setData({
                            isLastPage: false
                        });
                    }
                    self.setData({
                        floatDisplay: "block",
                        postsList: self.data.postsList.concat(response.data.map(function (item) {
                            var strdate = item.date
                            if (item.category_name != null) {
                                item.categoryImage = "";
                            }
                            else {
                                item.categoryImage = "";
                            }
                            if (item.post_thumbnail_image == null || item.post_thumbnail_image == '') {
                                item.post_thumbnail_image = "https://www.bds110.com/wp-content/uploads/bdsmp-dacx/logo700.png";
                            }
                            item.date = util.cutstr(strdate, 10, 1);
                            // console.log(item);
                            return item;
                        })),
                    });
                    setTimeout(function () {
                        swan.hideLoading();
                    }, 900);
                }
                else {
                    if (response.data.code == "rest_post_invalid_page_number") {
                        self.setData({
                            isLastPage: true
                        });
                        swan.showToast({
                            title: '没有更多内容',
                            mask: false,
                            duration: 1500
                        });
                    }
                    else {
                        swan.showToast({
                            title: response.data.message,
                            duration: 1500
                        })
                    }
                }
            })
            .catch(function (response) {
                if (data.page == 1) {
                    self.setData({
                        showerror: "block",
                        floatDisplay: "none"
                    });

                }
                else {
                    console.log(response);
                    swan.showModal({
                        title: '加载失败',
                        content: '加载数据失败,请重试.',
                        showCancel: false,
                    });
                    self.setData({
                        page: data.page - 1
                    });
                }
            })
            .finally(function (response) {
                swan.hideLoading();
                
            });
    },

    //加载分页
    loadMore: function (e) {

        var self = this;
        if (!self.data.isLastPage) {
            self.setData({
                page: self.data.page + 1
            });
            //console.log('当前页' + self.data.page);
            this.fetchPostsData(self.data);
        }
        else {
            swan.showToast({
                title: '没有更多内容',
                mask: false,
                duration: 1000
            });
        }
    },
    // 跳转至查看文章详情
    redictDetail: function (e) {
        // console.log('查看文章');
        var id = e.currentTarget.id,
            url = '../detail/detail?id=' + id;
        swan.navigateTo({
            url: url
        })
    },
    //首页图标跳转
    onNavRedirect: function (e) {
        var redicttype = e.currentTarget.dataset.redicttype;
        var url = e.currentTarget.dataset.url == null ? '' : e.currentTarget.dataset.url;
        var appid = e.currentTarget.dataset.appid == null ? '' : e.currentTarget.dataset.appid;
        var extraData = e.currentTarget.dataset.extraData == null ? '' : e.currentTarget.dataset.extraData;
        if (redicttype == 'apppage') {//跳转到小程序内部页面         
            swan.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'webpage')//跳转到web-view内嵌的页面
        {
            url = '../webpage/webpage?url=' + url;
            swan.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'miniapp')//跳转到其他app
        {
            swan.navigateToMiniProgram({
                appId: appid,
                envVersion: 'release',
                path: url,
                extraData: extraData,
                success(res) {
                    // 打开成功
                },
                fail: function (res) {
                    console.log(res);
                }
            })
        }

    },
    // 跳转至查看小程序列表页面或文章详情页
    redictAppDetail: function (e) {
        var pages = getCurrentPages() //获取加载的页面
        var currentPage = pages[pages.length - 1] //获取当前页面的对象
        var url = currentPage.route //当前页面url
        console.log(url)
        console.log('查看文章');
        var id = e.currentTarget.id;
        var redicttype = e.currentTarget.dataset.redicttype;
        var url = e.currentTarget.dataset.url == null ? '' : e.currentTarget.dataset.url;
        var appid = e.currentTarget.dataset.appid == null ? '' : e.currentTarget.dataset.appid;

        if (redicttype == 'detailpage')//跳转到内容页
        {
            url = '../detail/detail?id=' + id;
            swan.navigateTo({
                url: url
            })
        }
        if (redicttype == 'apppage') {//跳转到小程序内部页面         
            swan.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'webpage')//跳转到web-view内嵌的页面
        {
            url = '../webpage/webpage?url=' + url;
            swan.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'miniapp')//跳转到其他app
        {
            swan.navigateToMiniProgram({
                appId: appid,
                envVersion: 'release',
                path: url,
                success(res) {
                    // 打开成功
                },
                fail: function (res) {
                    console.log(res);
                }
            })
        }
    },
    scroll: function () {

        let marginTop = this.data.animStyle['margin-top'] - this.data.moveDistance
        // console.log(marginTop)

        this.setData({
            animStyle: {
                transition: 'all 0.5s',
                'margin-top': marginTop
            }
        });
        //   console.log(this.data.copyList)
        setTimeout(() => {
            this.setData({
                scrollList: this.data.scrollList.concat(this.data.copyList[this.data.index])
            })
            if (this.data.index == this.data.listLenght - 1) {
                this.setData({
                    scrollList: this.data.copyList,
                    index: 0,
                    animStyle: {
                        transition: 'all 0.5s',
                        'margin-top': 0
                    }
                })
            } else {
                this.setData({
                    index: this.data.index + 1,
                    peoples: this.data.peoples + 1 > 197 ? 197 : this.data.peoples + 1
                })
            }

        }, 1000)
    }
})
